package example.dao.impl;

import example.dao.ActionDao;
import example.dto.*;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;


@Repository
public class ActionDaoImpl implements ActionDao {



    //插入主商品列表
    @Override
    public int insertPhone_Product(List<Product> productList) {
        String sql="insert into product(pname,pOptId) values(?,?);";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps,int i) throws SQLException {
                ps.setString(1,productList.get(i).getPname());
                ps.setString(2,productList.get(i).getPOptId());
            }

            @Override
            public int getBatchSize() {
                return productList.size();
            }
        }).length;
    }

    //查询主商品
    @Override
    public List<Product> findAll_Product() {
        String sql="select * from product";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Product.class));

    }

    //查询主商品的ID
    @Override
    public Integer findProduct_ID(String pName) {
        String sql="select pid from product where pName=?";
        try {
            return jdbcTemplate.queryForObject(sql,new Object[]{pName},Integer.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    //查询子商品
    @Override
    public List<Children> findAll_Children() {
        String sql="select * from children";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Children.class));
    }

    //插入子商品
    @Override
    public int insertPhone_Children(List<Children> childrenList) {
        String sql="insert into children(cName,cImageUrl,cLinkUrl,cOptId,cIdx,pid) values(?,?,?,?,?,?);";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps,int i) throws SQLException {
                ps.setString(1,childrenList.get(i).getCName());
                ps.setString(2,childrenList.get(i).getCImageUrl());
                ps.setString(3,childrenList.get(i).getCLinkUrl());
                ps.setString(4,childrenList.get(i).getCOptId());
                ps.setString(5,childrenList.get(i).getCIdx());
                ps.setString(6,childrenList.get(i).getPid());
            }

            @Override
            public int getBatchSize() {
                return childrenList.size();
            }
        }).length;
    }

    //删除所有子商品
    @Override
    public int deletePhone_Children() {
        String sql="truncate table children;";
        return jdbcTemplate.update(sql);
    }

    //更新子商品
    @Override
    public int updatePhone_Children(List<Children> childrenList) {
        return 0;
    }


    //插入子商品列表
    @Override
    public int insertPhone_ChildreninfoList(List<ChildrenInfoList> childrenInfoLists) {
        String sql="insert into childreninfoList(ci_imgUrl,ci_text,ci_goodsName,ci_price,ci_salesTip,ci_linkURL,cid) values(?,?,?,?,?,?,?);";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps,int i) throws SQLException {
                ps.setString(1,childrenInfoLists.get(i).getCi_imaUrl());
                ps.setString(2,childrenInfoLists.get(i).getCi_text());
                ps.setString(3,childrenInfoLists.get(i).getCi_goodsName());
                ps.setString(4,childrenInfoLists.get(i).getCi_price());
                ps.setString(5,childrenInfoLists.get(i).getCi_salesTip());
                ps.setString(6,childrenInfoLists.get(i).getCi_linkURL());
                ps.setString(7,childrenInfoLists.get(i).getCid());
            }

            @Override
            public int getBatchSize() {
                return childrenInfoLists.size();
            }
        }).length;
    }

    //插入子商品详情
    @Override
    public int insertPhone_Childreninfo(List<ChildrenInfo> childrenInfoList) {
        String sql="insert into childreninfo(ch_viewImageUrl,ch_goodsName,ch_groupPrice,ch_normalPrice,ch_oldGroupPrice,ch_groupTip,ch_sideSalesTip" +
                ",ch_canshu,ch_goodsID) values(?,?,?,?,?,?,?,?,?);";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1,childrenInfoList.get(i).getCh_viewImageUrl());
                ps.setString(2,childrenInfoList.get(i).getCh_goodsName());
                ps.setString(3,childrenInfoList.get(i).getCh_groupPrice());
                ps.setString(4,childrenInfoList.get(i).getCh_normalPrice());
                ps.setString(5,childrenInfoList.get(i).getCh_linePrice());
                ps.setString(6,childrenInfoList.get(i).getCh_groupTip());
                ps.setString(7,childrenInfoList.get(i).getCh_sideSalesTip());
                ps.setString(8,childrenInfoList.get(i).getCh_canshu());
                ps.setString(9,childrenInfoList.get(i).getCh_goodsID());
            }

            @Override
            public int getBatchSize() {
                return childrenInfoList.size();
            }
        }).length;
    }


    //查询商品详情
    @Override
    public List<ChildrenInfo> findAll_ChildrenInfo() {
        String sql="select * from childreninfo";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(ChildrenInfo.class));
    }


    @Override
    public List<String> findChildrenInfo_ID(String id) {
        String sql="select ch_goodsID from childreninfo where ch_goodsID=?";
        try {
            return jdbcTemplate.queryForList(sql,new Object[]{id},String.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;
}
